package cn.tianyigps.tianyigpsapiv2.service;

import cn.tianyigps.tianyigpsapiv2.dao.EquipmentHistoryDao;
import cn.tianyigps.tianyigpsapiv2.dao.EquipmentLastDataDao;
import cn.tianyigps.tianyigpsapiv2.param.dto.EquipmentDTO;
import cn.tianyigps.tianyigpsapiv2.utils.MapTransformUtils;
import cn.tianyigps.tianyigpspublic.bean.EquipmentHistory;
import cn.tianyigps.tianyigpspublic.bean.EquipmentLastData;
import cn.tianyigps.tianyigpspublic.param.constant.EquipmentReceiveConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: LiuHao
 * @Descirption:
 * @Date: 2019/4/19_15:02
 */
@Service
public class EquipmentService {

    @Autowired
    private EquipmentHistoryDao equipmentHistoryDao;

    @Autowired
    private EquipmentLastDataDao equipmentLastDataDao;


//    public List<EquipmentModel> locus(EquipmentDTO equipmentDTO) {
//        List<EquipmentModel> result = new ArrayList();
//        //1头一天
//        List<EquipmentModel> head = equipmentDao.findByDayTo24AndEquipmentId(equipmentDTO.getStart(),equipmentDTO.getEquipmentId());
//        //2中间全查
//        Date startDay = TimeUtil.getDateAddDays(equipmentDTO.getStart(), 1);
//        Date endDay = TimeUtil.getDateSubtractDays(equipmentDTO.getEnd(), 1);
//        List<String> days = TimeUtil.getDiffDays(startDay, endDay);//获取日期时间段 掐头去尾
//        List<EquipmentModel> list = equipmentDao.findByDaysAndEquipmentId(days,equipmentDTO.getEquipmentId());
//        //3尾一天
//        List<EquipmentModel> tail = equipmentDao.findBy0ToDayAndEquipmentId(equipmentDTO.getEnd(),equipmentDTO.getEquipmentId());
//        result.addAll(head);
//        result.addAll(list);
//        result.addAll(tail);
//        if("bd09".equals(equipmentDTO.getMapType())){
//            result.stream().forEach(equipmentModel -> {
//                Point point = MapTransformUtils.gcj02tobd09(equipmentModel.getGpsLongitude(),equipmentModel.getGpsLatitude());
//                equipmentModel.setGpsLongitude(point.getX());
//                equipmentModel.setGpsLatitude(point.getY());
//            });
//        }
//
//        return result;
//    }

    public List<EquipmentHistory> locus(EquipmentDTO equipmentDTO) {
        List<EquipmentHistory> list = equipmentHistoryDao.findByDateTimeAndEquipmentId(equipmentDTO.getStart(),
                equipmentDTO.getEnd(),equipmentDTO.getEquipmentId());
        if("bd09".equals(equipmentDTO.getMapType())){
            list.stream().forEach(equipmentHistory -> {
                Map maps = equipmentHistory.getMaps();
                Point point = MapTransformUtils.gcj02tobd09((double)maps.get(EquipmentReceiveConstant.Longitude),
                        (double)maps.get(EquipmentReceiveConstant.Latitude));
                maps.put(EquipmentReceiveConstant.Longitude,point.getX());
                maps.put(EquipmentReceiveConstant.Latitude,point.getY());
            });
        }
        return list;
    }



    public EquipmentLastData lastData(String equipmentId, String mapType) {
        EquipmentLastData equipmentLastData = equipmentLastDataDao.lastData(equipmentId);
        if("bd09".equals(mapType)){
            Map maps = equipmentLastData.getMaps();
            Point point = MapTransformUtils.gcj02tobd09((double)maps.get(EquipmentReceiveConstant.Longitude),
                    (double)maps.get(EquipmentReceiveConstant.Latitude));
            maps.put(EquipmentReceiveConstant.Longitude,point.getX());
            maps.put(EquipmentReceiveConstant.Latitude,point.getY());
        }
        return equipmentLastData;
    }

}
